package com.liuya.safe.system.menu.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.github.pagehelper.PageInfo;
import com.liuya.base.BaseController;
import com.liuya.common.WebUtil;
import com.liuya.safe.system.menu.service.MenuManagerService;
import com.liuya.safe.model.SafeMenu;

/**
 * 菜单管理
 * @author luzhenxing
 * @date 20160826
 */
@RestController
@Api(value = "菜单管理", description = "菜单管理")
@RequestMapping(value = "menuMng")
public class MenuManagerContrller extends BaseController{
	
	 @Autowired
	 MenuManagerService menuService;
	 
	 // 查询会话
	    @ApiOperation(value = "查询会话") 
	    @RequestMapping(value = "/list")
	    public PageInfo<SafeMenu> list(HttpServletRequest request, ModelMap modelMap){
	        //http://localhost:8080/roleMng/list.do
	        Map<String, Object> params = WebUtil.getParameters(request);
	        Object _pageNo = params.get("pageNo"); 
	        params.put("pageNum", _pageNo);  
	        PageInfo<SafeMenu> list = menuService.selectPage(params);
	        Long count =  menuService.getPageListBySearchCount(params);  
	        list.setTotal(count);
			
	        return list;
	    }
	    
	    @RequestMapping(value = "/insertRole")
	    @ResponseBody
	    public String insertSafeRole(@ModelAttribute SafeMenu safeMenu) {
	    	 try{
	    		 menuService.add(safeMenu); 
	    	 }catch(Exception e){
	    		 return "fail";
	    	 } 
			return "success"; 
	    }
	    
	    @RequestMapping(value = "/updateRole")
	    @ResponseBody
	    public String updateRole(@ModelAttribute SafeMenu safeMenu) {
	    	try{
	    		menuService.update(safeMenu);
	    	}catch(Exception e){
	    		 return "fail";
	    	} 
	    	return "success";
	    } 
	    
	    @RequestMapping(value = "/getDetailByPk")
	    @ResponseBody
	    public SafeMenu getDetailByPk(String pk){
	    	return menuService.selectById(pk);
	    }
	    
	    @RequestMapping(value = "/deleteByPk")
	    @ResponseBody
	    public String deleteByPk(String pk){
	    	try {
	    		menuService.deleteById(pk);
	    	}catch(Exception e){
	    		return "fail";
	    	}
	    	return "success";
	    }
	    
	 
}
